#include "biotablewidget.h"
#include "attributeset.h"

BioTableWidget::BioTableWidget(QString fn, QGraphicsItem *parent) :
    filename(fn), QGraphicsWidget(parent)
{
    table = new QTableWidget( );
    table->setColumnCount(25);//number of columns, extra to allow for editing
    table->setRowCount(100);//number of rows, extra to allow for editing
    readFileIntoTable( filename );

    table->setVisible(true);//make sure table is visible

    proxy_table = new QGraphicsProxyWidget(this, Qt::Widget);//proxy table for graphics view architecture
    proxy_table->setWidget( table );
    proxy_table->setPos(0,0);
    QRectF pRect = boundingRect();
    QRectF proxyRect( pRect.x()+15, pRect.y()+15, pRect.width()-30, pRect.height()-30);//bounding rect of table slightly smaller than parent
    proxy_table->setGeometry( proxyRect );//set bounding rect
    proxy_table->setFlag( QGraphicsItem::ItemIsMovable);//movable in scene

    setFlag( QGraphicsItem::ItemIsMovable );

    createAttributeSets();
}

QRectF BioTableWidget::boundingRect() const
{
    return QRectF(-350, -300, 700, 600);
}

QPainterPath BioTableWidget::shape() const
{
    QPainterPath path;
    path.addRect(boundingRect());
    return path;
}

void BioTableWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    QColor background(175, 175, 175, 255 );
    painter->setBrush(background);

    QRectF bRect = boundingRect();
    bRect.adjust(1, 1, -1, -1);
    painter->drawRect(bRect);
}

bool BioTableWidget::readFileIntoTable(QString filename)
{
    QFile f(filename);//get file

    if( !f.exists() )//make sure it exists
    {
        qDebug() << "file not found." << endl;
        return false;
    }

    if( ! f.open(QIODevice::ReadOnly))//make sure it can be opened
    {
        qDebug() << "file " << filename << " can't be opened." << endl;
        return false;
    }

    qDebug() << "file " << filename << " opened." << endl;

    QTextStream stream(&f);
    QString line;
    rowCount = 0;
    columnCount = 0;
    do {
         line = stream.readLine();
         //qDebug() << "line =  " << line << endl;
         QStringList list = line.split("\t", QString::SkipEmptyParts);//tab delimited file
         for(int column = 0; column<list.size(); column++)
         {
             QTableWidgetItem *newItem = new QTableWidgetItem(list.at(column) );//set next table item to elt in file
             table->setItem(rowCount, column, newItem);
             if( column > columnCount )
                 columnCount = column;//set column count to greatest number of columns in a row
         }
         rowCount++;
     } while (!stream.atEnd() );

    return true;
}

void BioTableWidget::getElements(int col, QList<QString>& elts)
{
    for(int row = 1; row < rowCount; row++)
    {
        elts.push_back( table->item(row, col)->text() );
    }
}

void BioTableWidget::createAttributeSets()
{
    //get attributes for network
    QList<QString> genes;//get data vals
    getElements( 0, genes);

    for( int col = 2; col <= 4; col++)
    {
        QString name = table->item(0, col)->text();
        QList<QString> atts;
        getElements( col, atts);//get attribute values
        QualAttributeSet* q = new QualAttributeSet( name, genes, atts, this );//create a new attribute set
        attributeSets.push_back( q );//and add it to the array
        attributeSets[attributeSets.size()-1]->setVisible(false);//turn visibility off for now
    }

    //technically, here i need to make sure that the colors are the same for attribute
    //sets with the same color values
    //but, i have riged things so that colors will be set to the correct values
    //later, i'll have to add a 'compareDomains' function
    //that if it is equal, i pass one color vector to the other attribute set

}

void BioTableWidget::getAttributeSets(QVector< QPointer <QualAttributeSet> >& attSets)
{
    qDebug() << attributeSets.size() << endl;
    //create a deep copy of each attribute set to add to vector
    for(int i = 0; i < attributeSets.size(); i++)
    {
        attSets.push_back( new QualAttributeSet( *(attributeSets[i]) ) );
    }
}


